

public class Robot implements Runnable{

    public String robotID;
    protected RobotComm robotComm;
    protected DBComm dbComm;
    protected char initHeading;
    protected char heading;
    protected Point initPoint;
    protected Point point;
    protected int leftPath, fwdPath, rightPath;
    protected boolean moving;
    protected boolean isAlive;

    public Robot(String id, RobotComm robotComm, char initHeading, Point initPoint, int[] path) {
        this.robotID = id;
        this.robotComm = robotComm;
        dbComm = new DBComm(this);
        this.initHeading = initHeading;
        heading = initHeading;
        this.initPoint = initPoint;
        point = initPoint;
        leftPath = path[0];
        fwdPath = path[1];
        rightPath = path[2];
        isAlive = true;
    }

    public void run(){        
        byte[] data;
        while(isAlive){
            while((data = dbComm.read()) == null){}
            robotComm.send(data);
            System.out.println("Sending {" + data[0] + "," + data[1] + "}");
            data = robotComm.receive();
            if (data[0] != 0) {
                System.out.println("Received " + data[0] + "," + data[1] + "," + data[2] + "," + data[3] + "," + data[4] + "," + data[5] + " len: " + data.length);
                update(data);
                dbComm.write();
            }
        }
    }

    public void initRobot(){
        robotComm.send(new byte[]{32, (byte) heading, (byte) point.x, (byte) point.y});
    }
    
    public void initDB(){
        dbComm.connect();
        dbComm.write();
    }

    public void update(boolean moving){
        moving = true;
    }

    public void update(byte[] data){
        heading = (char) data[0];
        point.update(data[1], data[2]);
        leftPath = data[3];
        fwdPath = data[4];
        rightPath = data[5];
        moving = false;
    }

    public void stop(){
        robotComm.close();
        dbComm.disconnect();
        isAlive = false;
    }
}
